﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
namespace GameEditor
{
    public static class AssetBundleMenu
    {
        [MenuItem("AssetBundle/ClearMarks", false, 1)]
        public static void ClearMarks()
        {
            AssetBundleBuilder.ClearMarks();
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 标记资源
        /// </summary>
        [MenuItem("AssetBundle/MarkAssets", false, 2)]
        public static void MarkAssets()
        {
            AssetDatabase.RemoveUnusedAssetBundleNames();
            Debug.Log("开始标记资源!");
            DateTime oldTime = DateTime.Now;
            #region 托管资源
            //字体
            AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Fonts");
            //UI图集资源 
            AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/UIAtlas");
            //装备
            //AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/Equips/Weapon");
            //AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/Equips/Wing");
            //AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/Equips/YiFu");
            //NPC
            //AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/Npcs");
            //怪物
            //AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/Monsters");
            //UI角色资源
            AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/PlayerModel/ChiBang");
            AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/PlayerModel/ShenJie");
            AssetBundleBuilder.MarkSubFloderOneBundle("RawResources/Prefabs/PlayerModel/WuQi");
            AssetBundleBuilder.MarkOneFloderOneBundle("RawResources/Prefabs/PlayerModel/YiFu");
            AssetBundleBuilder.MarkOneFloderOneBundle("RawResources/Prefabs/PlayerModel/ZuoQi");


            //动画
            //AssetBundleBuilder.MarkOneFloderOneBundle("Editor/AnimationTool/AnimTemplate");
            #endregion

            #region 非托管资源
            //全局材质包
            AssetBundleBuilder.MarkOneFloderOneBundle("ManagedResources/Globals/Materials");
            //全局Shader包
            AssetBundleBuilder.MarkOneFloderOneBundle("ManagedResources/Globals/Shaders");
            //UI图集
            AssetBundleBuilder.MarkOneAssetOneBundle("ManagedResources/UIAtlas", false);
            //背景音乐
            AssetBundleBuilder.MarkOneAssetOneBundle("ManagedResources/Audio/BGM", false);
            //配置
            AssetBundleBuilder.MarkOneAssetOneBundle("ManagedResources/Configs", false);
            //地图
            AssetBundleBuilder.MarkSubFloderOneBundle("ManagedResources/Map/");
            //预制体
            AssetBundleBuilder.MarkOneAssetOneBundle("ManagedResources/Prefabs", true);
            #endregion
            //标记文件之后生成资源配置
            ResPathConfigTool.CreateResPathConfig();
            TimeSpan span = DateTime.Now.Subtract(oldTime);
            Debug.Log("标记资源完毕，本次标记资源耗时:" + span.TotalSeconds + "秒!");
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包IOS
        /// </summary>
        [MenuItem("AssetBundle/Build/IOS/None", false, 1)]
        public static void BuildIOS()
        {
            string exportPath = GetExportPath(BuildTarget.iOS);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.iOS, NoneOptions);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包IOS
        /// </summary>
        [MenuItem("AssetBundle/Build/IOS/LZ4", false, 2)]
        public static void BuildIOSLZ4()
        {
            string exportPath = GetExportPath(BuildTarget.iOS);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.iOS, LZ4Options);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }


        /// <summary>
        /// 整体打包IOS
        /// </summary>
        [MenuItem("AssetBundle/Build/IOS/LZMA", false, 3)]
        public static void BuildIOSLZMA()
        {
            string exportPath = GetExportPath(BuildTarget.iOS);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.iOS, LZMAOptions);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包Android
        /// </summary>
        [MenuItem("AssetBundle/Build/Android/None", false, 3)]
        public static void BuildAndroid()
        {
            string exportPath = GetExportPath(BuildTarget.Android);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.Android, NoneOptions);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包Android
        /// </summary>
        [MenuItem("AssetBundle/Build/Android/LZ4", false, 3)]
        public static void BuildAndroidLZ4()
        {
            string exportPath = GetExportPath(BuildTarget.Android);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.Android, LZ4Options);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包Android
        /// </summary>
        [MenuItem("AssetBundle/Build/Android/LZMA", false, 3)]
        public static void BuildAndroidLZMA()
        {
            string exportPath = GetExportPath(BuildTarget.Android);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.Android, LZMAOptions);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包Windows64
        /// </summary>
        [MenuItem("AssetBundle/Build/Windows64/None", false, 1)]
        public static void BuildWindows64()
        {
            string exportPath = GetExportPath(BuildTarget.StandaloneWindows64);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.StandaloneWindows64, NoneOptions);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包Windows64
        /// </summary>
        [MenuItem("AssetBundle/Build/Windows64/LZ4", false, 2)]
        public static void BuildWindows64LZ4()
        {
            string exportPath = GetExportPath(BuildTarget.StandaloneWindows64);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.StandaloneWindows64, LZ4Options);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 整体打包Windows64
        /// </summary>
        [MenuItem("AssetBundle/Build/Windows64/LZMA", false, 3)]
        public static void BuildWindows64LZMA()
        {
            string exportPath = GetExportPath(BuildTarget.StandaloneWindows64);
            AssetBundleBuilder.BuildAssetBundles(exportPath, BuildTarget.StandaloneWindows64, LZMAOptions);
            AssetBundleBuilder.ClearManifest(exportPath);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源IOS
        /// </summary>
        [MenuItem("Assets/BuildAB/IOS/None",false,1)]
        public static void BuildSelectIOS()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(NoneOptions, BuildTarget.iOS);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源IOS
        /// </summary>
        [MenuItem("Assets/BuildAB/IOS/LZ4",false,2)]
        public static void BuildSelectIOSLZ4()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(LZ4Options, BuildTarget.iOS);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源IOS
        /// </summary>
        [MenuItem("Assets/BuildAB/IOS/LZMA",false,3)]
        public static void BuildSelectIOSLZMA()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(LZMAOptions, BuildTarget.iOS);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源Android
        /// </summary>
        [MenuItem("Assets/BuildAB/Android/None",false,1)]
        public static void BuildSelectAndroid()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(NoneOptions, BuildTarget.Android);
            AssetDatabase.Refresh();
        }


        /// <summary>
        /// 打包选中资源Android
        /// </summary>
        [MenuItem("Assets/BuildAB/Android/LZ4",false,2)]
        public static void BuildSelectAndroidLZ4()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(LZ4Options, BuildTarget.Android);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源Android
        /// </summary>
        [MenuItem("Assets/BuildAB/Android/LZMA",false,3)]
        public static void BuildSelectAndroidLZMA()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(LZMAOptions, BuildTarget.Android);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源Windows64
        /// </summary>
        [MenuItem("Assets/BuildAB/Windows64/None",false,1)]
        public static void BuildSelectWindows64()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(NoneOptions, BuildTarget.StandaloneWindows64);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源Windows64
        /// </summary>
        [MenuItem("Assets/BuildAB/Windows64/LZ4",false,2)]
        public static void BuildSelectWindows64LZ4()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(LZ4Options, BuildTarget.StandaloneWindows64);
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 打包选中资源Windows64
        /// </summary>
        [MenuItem("Assets/BuildAB/Windows64/LZMA",false,3)]
        public static void BuildSelectWindows64LZMA()
        {
            AssetBundleBuilder.BuildSelectAssetBundles(LZMAOptions, BuildTarget.StandaloneWindows64);
            AssetDatabase.Refresh();
        }


        /// <summary>
        /// 不压缩资源
        /// </summary>
        public static BuildAssetBundleOptions NoneOptions
        {
            get
            {
                return BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.UncompressedAssetBundle;
            }
        }

        /// <summary>
        /// 块压缩
        /// </summary>
        public static BuildAssetBundleOptions LZ4Options {
            get {
                return BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression;
            }
        }

        /// <summary>
        /// 默认LZMA压缩
        /// </summary>
        public static BuildAssetBundleOptions LZMAOptions {
            get
            {
                return BuildAssetBundleOptions.DeterministicAssetBundle;
            }
        }

        /// <summary>
        /// 获取导出路径
        /// </summary>
        /// <returns>The export path.</returns>
        /// <param name="target">Target.</param>
        public static string GetExportPath(BuildTarget target)
        {
            string path = Application.streamingAssetsPath;
            switch (target)
            {
                case BuildTarget.Android:
                    path += "/Android";
                    break;
                case BuildTarget.iOS:
                    path += "/IOS";
                    break;
                case BuildTarget.StandaloneWindows:
                case BuildTarget.StandaloneWindows64:
                    path += "/Windows";
                    break;
                case BuildTarget.StandaloneOSXIntel:
                case BuildTarget.StandaloneOSXIntel64:
                case BuildTarget.StandaloneOSXUniversal:
                    path += "/OSX";
                    break;
                default:
                    path += "/Other";
                    break;
            }
            return path;
        }
    }
}
